package genedit.controller.interfaceActions;

import genedit.gui.GFileChooser;
import genedit.gui.GMainFrame;
import genedit.model.Workspace;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.KeyStroke;

/**
 * Akcija zadužena za biranje putanje trenutnog workspace-a.
 * 
 * @author Nikola Luburić
 * @version 1.0 19/11/13
 */
public class GSwitchWorkspaceA extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected static GSwitchWorkspaceA instance = null;

	/**
	 * Create the action with default look and feel.
	 */
	protected GSwitchWorkspaceA() {
		putValue(NAME, "Switch Workspace");
		putValue(SHORT_DESCRIPTION, "Switch Workspace");
		putValue(SMALL_ICON, new ImageIcon("resources/icons/interact.png"));
		putValue(MNEMONIC_KEY, KeyEvent.VK_K);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control K"));
	}

	/**
	 * Akcija otvara dijalog koji omogućuje korisniku da izabere direktorijum
	 * novog workspace-a.
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		GFileChooser.getInstance(true).setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		if (GFileChooser.getInstance(true).showOpenDialog(GMainFrame.getInstance()) == JFileChooser.APPROVE_OPTION) {
			File cFile = GFileChooser.getInstance(true).getSelectedFile();
			Workspace.getInstance().setPath(cFile);
			GMainFrame.getInstance().loadWorkspace();
		} else
			GFileChooser.getInstance(true).setCurrentDirectory(Workspace.getInstance().getPath());
	}

	public static GSwitchWorkspaceA getInstance() {
		if (instance == null)
			instance = new GSwitchWorkspaceA();
		return instance;
	}
}
